GLenum texFormat;

BOOL checkSize(int x)
{
  if (x==64||x==128||x==256||x==512)
    return TRUE;
  else
	return FALSE;
}

unsigned char *getRGBA (FILE *s, int size)
{
  unsigned char *rgba;
  unsigned char temp;
  int bread;
  int i;

  rgba=malloc(size*4);

  if (rgba==NULL) return 0;

  bread=fread(rgba, sizeof (unsigned char), size * 4, s);

  if (bread != size * 4) {
    free (rgba);
    return 0;
  }

  for (i = 0; i < size * 4; i += 4 ) {
    temp = rgba[i];
    rgba[i] = rgba[i + 2];
    rgba[i + 2] = temp;
  }

  texFormat = GL_RGBA;
  return rgba;
}

unsigned char *getRGB (FILE *s, int size)
{
  unsigned char *rgb;
  unsigned char temp;
  int bread;
  int i;

  rgb = malloc (size * 3);

  if (rgb == NULL) return 0;

  bread = fread (rgb, sizeof (unsigned char), size * 3, s);

  if (bread != size * 3) {
    free (rgb);
    return 0;
  }

  for (i = 0; i < size * 3; i += 3)
  {
    temp = rgb[i];
    rgb[i] = rgb[i + 2];
    rgb[i + 2] = temp;
  }

  texFormat = GL_RGB;

  return rgb;
}

unsigned char *getGray(FILE *s, int size)
{
  unsigned char *grayData;
  int bread;

  grayData = malloc (size);

  if (grayData == NULL) return 0;

  bread = fread (grayData, sizeof (unsigned char), size, s);

  if (bread != size) {
    free (grayData);
    return 0;
  }

  texFormat = GL_ALPHA;

  return grayData;
}

char *getData (FILE *s, int sz, int iBits)
{
  if (iBits == 32)
    return getRGBA (s, sz);
  else
    if (iBits == 24)
      return getRGB (s, sz);
    else
      if (iBits == 8) return getGray (s, sz);
  return getGray (s, sz);
}

BOOL load_tga_texture(char *filename, int *id)
{
  unsigned char type[4];
  unsigned char info[7];
  unsigned char *imageData = NULL;
  char buffer[80];
  int widht, height;
  int imageBits, size;
  FILE *file;

  if (!filename||!id) return FALSE;

  if (!(file=fopen(filename, "rb")))
  {
    sprintf(buffer,"Can't open file %s.", filename);
    MessageBox(hwnd, buffer, "ERROR",MB_OK|MB_ICONINFORMATION);
    return FALSE;
  }

  fread (&type, sizeof (char), 3, file);
  fseek (file, 12, SEEK_SET);
  fread (&info, sizeof (char), 6, file);

  if (type[1] != 0 || (type[2] != 2 && type[2] != 3))
  {
    sprintf(buffer,"File %s is bad image type.", filename);
    MessageBox(hwnd, buffer, "ERROR",MB_OK|MB_ICONINFORMATION);
    fclose(file);
    return FALSE;
  }

  widht = info[0] + info[1] * 256;
  height = info[2] + info[3] * 256;
  imageBits =	info[4];

  size = widht * height;

  if (!checkSize(widht) || !checkSize(height))
  {
    sprintf(buffer,"Image size %dX%d in file %s is not suportet.", widht, height, filename);
    MessageBox(hwnd, buffer, "ERROR",MB_OK|MB_ICONINFORMATION);
    fclose(file);
    return FALSE;
  }

  if (imageBits != 32 && imageBits != 24 && imageBits != 8)
  {
    sprintf(buffer, "File %s has bad color depth.", filename);
    MessageBox(hwnd, buffer, "ERROR",MB_OK|MB_ICONINFORMATION);
    fclose(file);
    return FALSE;
  }
  imageData = getData (file, size, imageBits);

  if (imageData == NULL)
  {
    sprintf(buffer,"File %s has bad image data.", filename);
    MessageBox(hwnd, buffer, "ERROR",MB_OK|MB_ICONINFORMATION);
    fclose(file);
    return FALSE;
  }
  fclose (file);


  glGenTextures(1, id);
  glBindTexture (GL_TEXTURE_2D, *id);
  glPixelStorei (GL_UNPACK_ALIGNMENT, 1);
  glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
  glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
  glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
  glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
  glTexEnvf (GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
  //gluBuild2DMipmaps(GL_TEXTURE_2D, texFormat, leveys, korkeus, texFormat, GL_UNSIGNED_BYTE, imageData);
  glTexImage2D (GL_TEXTURE_2D, 0, texFormat, widht, height, 0, texFormat, GL_UNSIGNED_BYTE, imageData);

  free (imageData);

  return TRUE;
}

